package com.luke.source;

import com.luke.netty.sample.NettyServerChannelHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.AttributeKey;

/**
 * Netty服务端
 */
public class Server {

    public static void main(String[] args) throws Exception{

        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(bossGroup,workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.TCP_NODELAY,true) //TCP协议参数配置
                    .childAttr(AttributeKey.newInstance("childAttr"),"childAttr")
                    //.handler(new ServerHandler())
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            //TODO
                            socketChannel.pipeline()
                                    .addLast(new InBoundHandlerA())
                                    .addLast(new InBoundHandlerB())
                                    .addLast(new InBoundHandlerC())
                                    .addLast(new OutBoundHandlerA())
                                    .addLast(new OutBoundHandlerB())
                                    .addLast(new OutBoundHandlerC());
                        }
                    });
            ChannelFuture future = bootstrap.bind(8888).sync();
            future.channel().closeFuture().sync();//同步监听退出
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }

    }

}
